<?php

declare(strict_types=1);

namespace App\CustHub\Controller\Admin;

use App\CustHub\Controller\BaseController;
use App\CustHub\Request\ChCaseRequest;
use App\CustHub\Service\ChCaseService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Mine\Annotation\Auth;
use Mine\Annotation\OperationLog;
use Mine\Annotation\Permission;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: "ch/admin/case"),auth]
class ChCaseController extends BaseController
{
    #[Inject]
    protected ChCaseService $service;
    /**
     * 获取客户案例详细列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping('index'),Permission("ch:admin:case:index")]
    public function index():ResponseInterface
    {
        return $this->success($this->service->getCaseList($this->request->all()));
    }

    /**
     * 新增案例
     * @param ChCaseRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("save"), Permission("ch:admin:case:save"), OperationLog]
    public function save(ChCaseRequest $request): ResponseInterface
    {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 更新案例
     * @param int $id
     * @param ChCaseRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PutMapping("update/{id}"), Permission("ch:admin:case:update"), OperationLog]
    public function update(int $id, ChCaseRequest $request): ResponseInterface
    {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除案例
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[DeleteMapping("delete"), Permission("ch:admin:case:delete"), OperationLog]
    public function delete(): ResponseInterface
    {
        return $this->service->delete((array) $this->request->input('ids', [])) ? $this->success() : $this->error();
    }

    /**
     * 后台案例分类列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping('industry'),Permission("ch:admin:case:industryList")]
    public function caseIndustry()
    {
        return $this->success($this->service->caseIndustry($this->request->all()));
    }

    /**
     * 新增案例分类
     * @param ChCaseRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("addIndustry"), Permission("ch:admin:case:addIndustry"), OperationLog]
    public function addIndustry(ChCaseRequest $request): ResponseInterface
    {
        return $this->success(['id' => $this->service->addIndustry($request->all())]);
    }

    /**
     * 更新案例分类
     * @param int $industry
     * @param ChCaseRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PutMapping("updateIndustry/{industry}"), Permission("ch:admin:case:updateIndustry"), OperationLog]
    public function updateIndustry(int $industry, ChCaseRequest $request): ResponseInterface
    {
        return $this->service->updateIndustry($industry, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除案例分类
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[DeleteMapping("deleteIndustry"), Permission("ch:admin:server:deleteIndustry"), OperationLog]
    public function deleteIndustry(): ResponseInterface
    {
        return $this->service->deleteIndustry((array) $this->request->input('ids', [])) ? $this->success() : $this->error();
    }

}